package sqlite;

import java.io.*;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import nodosRegex.PreNodo;


public class DB {
	String link;
	int umbral = 2;
	PreNodo aux;
	ArrayList<PreNodo> lista_AG = new ArrayList<PreNodo>();
	ArrayList<PreNodo> lista_AC = new ArrayList<PreNodo>();
	
	public static void main(String[] args) {
		new DB();
	}

	public DB() {
		try {
			String line;
			String line2;

			Process a = Runtime.getRuntime().exec("cmd /c cd \"C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\" & dir /s /a \"places.sqlite\"");
			BufferedReader bri2 = new BufferedReader(new InputStreamReader(a.getInputStream()));
			while ((line2 = bri2.readLine()) != null) {
				Pattern p = Pattern.compile("^.*Directory of (.*).*");
				Matcher m = p.matcher(line2);
				if (m.find()){
				link=m.group(1);
				}
			}		
			bri2.close();
			a.waitFor();
			
			Process p = Runtime.getRuntime().exec("cmd /c sqlite3.exe \""+link+"\\places.sqlite\" \"select url, visit_count from moz_places where url like '%http://es.wikipedia%' AND last_visit_date > (strftime('%s','now','-5 hours'))*1000000\"");
			BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
			while ((line = bri.readLine()) != null) {
				//System.out.println(line);
				String data[] = line.split("\\|");

				aux=new PreNodo(data[0], Integer.parseInt(data[1]));
				//System.out.println("AC: "+data[0]+" "+Integer.parseInt(data[1]));
		    	lista_AC.add(aux);
		    	
				if(Integer.parseInt(data[1]) > umbral){
					//System.out.println("AG: "+data[0]+" "+Integer.parseInt(data[1]));
					aux=new PreNodo(data[0], Integer.parseInt(data[1]));
			    	lista_AG.add(aux);
				}
			}
			bri.close();
			p.waitFor();
			
		} catch (Exception e) {
		}
	}

	public ArrayList<PreNodo> getLista_AG() {
		return lista_AG;
	}

	public void setLista_AG(ArrayList<PreNodo> lista_AG) {
		this.lista_AG = lista_AG;
	}

	public ArrayList<PreNodo> getLista_AC() {
		return lista_AC;
	}

	public void setLista_AC(ArrayList<PreNodo> lista_AC) {
		this.lista_AC = lista_AC;
	}
}